package
{
	import com.greensock.TweenLite;
	import com.greensock.easing.Cubic;
	import com.greensock.plugins.HexColorsPlugin;
	
	import flash.events.TimerEvent;
	import flash.geom.Point;
	import flash.utils.Timer;

	public class TileDropGroup
	{
		public static const DROP_SPEED:Number = .5;
		public static const DROP_STYLE_BULK_NEW:String = 'bulk_new';
		public static const DROP_STYLE_REPLENISH:String = 'replinish';
		
		private var m_timeLine:Number;
		public var dropStyle:String;
		private var m_tiles:Vector.<Tile>;
		private var m_startingLocations:Vector.<Point>;
		private var m_endingLocations:Vector.<Point>;
		
		private var m_onComplete:Function;
		public function TileDropGroup(tiles:Vector.<Tile>)
		{
			m_tiles = tiles;
			m_timeLine = 0;
		}
		
		public function addTile(tile:Tile):void
		{
			m_tiles.push(tile);
		}
		
		public function go(onComplete:Function = null):void
		{
			if(onComplete == null) onComplete = function():void{}
			m_onComplete = onComplete;
				
			switch(dropStyle)
			{
				case DROP_STYLE_BULK_NEW:
					dropStyleBulkNew();
					break;
				case DROP_STYLE_REPLENISH:
					dropStyleReplenish();
					break;
			}
						
		}
		
		private function dropStyleReplenish():void
		{
			m_startingLocations = new Vector.<Point>;
			for each(var tile:Tile in m_tiles)
			{
				m_startingLocations.push(new Point(tile.display.x, tile.display.y));
			}
			
			TweenLite.to(this, DROP_SPEED, {timeLine: 1, ease: Cubic.easeIn, onComplete:function():void{
				for each(var tile:Tile in m_tiles)
				{
					tile.interactable = true;
				}
			}});			
		}
		
		private function dropStyleSingleNew():void
		{			
			TweenLite.to(this, DROP_SPEED, {timeLine: 1, ease: Cubic.easeIn, onComplete:function():void{
				for each(var tile:Tile in m_tiles)
				{
					tile.interactable = true;
				}
			}});
		}
		
		private function dropStyleBulkNew():void
		{
			var dropGroups:Vector.<TileDropGroup> = new Vector.<TileDropGroup>;
			var tdg:TileDropGroup;
			
			for(var i:int = 0; i < BJClone.HEIGHT; i++)
			{
				tdg = new TileDropGroup(new Vector.<Tile>);
				tdg.dropStyle = TileDropGroup.DROP_STYLE_REPLENISH;
				dropGroups.push(tdg);
			}
			
			for each(var tile:Tile in m_tiles)
			{
				var tiles:Vector.<Tile> = new Vector.<Tile>;
				dropGroups[tile.y].addTile(tile);
				var startingRenderPoint:Point = new Point(tile.x*tile.tileSize, tile.y*tile.tileSize - tile.tileSize*BJClone.HEIGHT);
				var endRenderPoint:Point = new Point(tile.x*tile.tileSize, tile.y*tile.tileSize);
				tile.display.y = startingRenderPoint.y;
				tile.display.x = startingRenderPoint.x;
			}
			
			var timer:Timer = new Timer(100);
			timer.addEventListener(TimerEvent.TIMER, function(te:TimerEvent):void{
				if(dropGroups.length > 0)
				{
					tdg = dropGroups.pop();
					tdg.go();
				}
				else
				{
					timer.stop();
					m_onComplete();
				}
			});
			timer.start();
		}
		
		public function set timeLine(val:Number):void
		{
			var i:int, il:int;
			var tile:Tile;
			var startingRenderPoint:Point;
			var endRenderPoint:Point;
			
			switch(dropStyle)
			{
				case DROP_STYLE_REPLENISH:
					for(i = 0, il = m_tiles.length; i < il; i++)
					{
						tile = m_tiles[i];
						startingRenderPoint = m_startingLocations[i];
						endRenderPoint = new Point(tile.x*tile.tileSize, tile.y*tile.tileSize);
						tile.display.y = startingRenderPoint.y + ( endRenderPoint.y - startingRenderPoint.y)*val;
						tile.display.x = startingRenderPoint.x + ( endRenderPoint.x - startingRenderPoint.x)*val;
					}					
					break;
			}
			
			
			m_timeLine = val;
		}
		
		public function get timeLine():Number
		{
			return m_timeLine;
		}
	}
}